//
// Created by WenDaojiang on 15/11/21.
// Copyright (c) 2015 文道江. All rights reserved.
//

#import "HybridApplication.h"
#import "HybridBridge.h"

@implementation HybridApplication

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    _launchOptions = launchOptions;
    [HybridEventBus defaultEventBus].launchOptions = launchOptions;
    [[HybridEventBus defaultEventBus] postData:launchOptions Tag:APP_EVENT_LAUNCHING_FINISH];
    return YES;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    NSLog(@"handleOpenURL %@", url);
    return YES;
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [[HybridEventBus defaultEventBus] postData:nil Tag:APP_EVENT_BECOME_ACTIVE];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    [[HybridEventBus defaultEventBus] postData:nil Tag:APP_EVENT_WILL_RESIGN_ACTIVE];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [[HybridEventBus defaultEventBus] postData:nil Tag:APP_EVENT_DID_ENTER_BACKGROUND];

    UIApplication* app = [UIApplication sharedApplication];
    UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        sleep(1);
        [app endBackgroundTask:bgTask];
    }];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    [[HybridEventBus defaultEventBus] postData:nil Tag:APP_EVENT_WILL_ENTER_FOREGROUND];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    NSLog(@"applicationWillTerminate: ==== E ====");
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    [[HybridEventBus defaultEventBus] postData:@{@"notificationSettings" : notificationSettings} Tag:APP_EVENT_REGISTER_USER_NOTIFICATION_SETTINGS];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [[HybridEventBus defaultEventBus] postData:@{@"deviceToken" : deviceToken} Tag:APP_EVENT_REGISTER_FOR_REMOTE_NOTIFICATIONS_WITH_DEVICE_TOKEN];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    [[HybridEventBus defaultEventBus] postData:@{@"error": error} Tag:APP_EVENT_FAIL_TO_REGISTER_FOR_REMOTE_NOTIFICATIONS_WITH_ERROR];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [[HybridEventBus defaultEventBus] postData:@{@"userInfo": userInfo} Tag:APP_EVENT_RECEIVE_REMOTE_NOTIFICATION];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
    [[HybridEventBus defaultEventBus] postData:@{@"userInfo" : userInfo, @"completionHandler" : completionHandler} Tag:APP_EVENT_RECEIVE_REMOTE_NOTIFICATION];
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    [[HybridEventBus defaultEventBus] postData:@{@"notification" : notification} Tag:APP_EVENT_RECEIVE_LOCAL_NOTIFICATION];
}

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
    [[HybridEventBus defaultEventBus] postData:@{@"completionHandler" : completionHandler} Tag:APP_EVENT_PERFORM_FETCH_WITH_COMPLETION_HANDLER];
}

- (void)forwardInvocation:(NSInvocation *)anInvocation {
    SEL aSelector = [anInvocation selector];
    if ([self respondsToSelector:aSelector]) {
        NSLog(@"forwardInvocation: %@", NSStringFromSelector(aSelector));
    } else {
        [super forwardInvocation:anInvocation];
    }
}

@end